package com.chanyachting.emall.util;

import javax.servlet.http.HttpServletRequest;

public class RequestUtil {
	
	private HttpServletRequest request;
	
	public static final String MSG_IN_REQUEST="MSG_IN_REQUEST";
	
	
	
	
	
	
	public RequestUtil(HttpServletRequest request){
		
		this.request=request;
		
	}
	
	
	public void setAttribute(String name ,Object ob){
		
		request.setAttribute(name, ob);
		
	}
	
	
	
	public Object getAttribute(String name ){
		
		return request.getAttribute(name);
		
	}
	
	
	
	public Object getAttribute(String name ,Object defOb){
		
		return this.getAttribute(name)==null?defOb:request.getAttribute(name);
		
	}
	
	
	
	public void setMsg(String msg){
		
		this.request.setAttribute(MSG_IN_REQUEST, msg);
		
	}
	
	
	public void setSuccessMsg(){
		
		this.setMsg("操作成功！");
		
	}
	
	public void setFailMsg(){
		
		this.setMsg("操作失败！请稍候重试。");
		
	}
	
	
	
	
	public String getMsg(){
		
		String msg="";
		
		
		if(this.request.getAttribute(MSG_IN_REQUEST)!=null){
			
			return this.request.getAttribute(MSG_IN_REQUEST).toString();
		}
		
		return msg;
	}
	
	
	
	
	public String getParameter(String args,String defValue){
		
		
		if(this.request.getParameter(args)!=null && !this.request.getParameter(args).trim().equals("")){
			
			return this.request.getParameter(args);
		}
		
		return defValue;
	}
	
	
	
	public Object getSessionObject(String args){
		
		return this.request.getSession().getAttribute(args);
	
	}
	
	
	public Object getSessionObject(String args,Object defObj){
		
		if(this.getSessionObject(args)!=null){
			
			return this.getSessionObject(args);
		}
		
		return defObj;
	
	}
	
	
	public void updateSessionObject(String args,Object obj){
		
		this.removeSessionObject(args);
		
		this.setSessionObject(args, obj);
	}
	
	
	public void removeSessionObject(String args){
		
		this.request.getSession().removeAttribute(args);
	}
	
	
	public void setSessionObject(String args,Object obj){
		
		this.removeSessionObject(args);
		
		this.request.getSession().setAttribute(args, obj);
	}
	
	
	//获得应用web访问路径
	public String getContextVirtualPath(){
		
		
		String temp="http://"+ request.getServerName()+":"+request.getServerPort()+request.getContextPath();
		
		if(request.getServerPort()==80){
			
			temp="http://"+ request.getServerName()+request.getContextPath();
		}
		
		return temp;
		
	}
	
	
	
	//获得应用的绝对路径
	public String getContextRealPath(){
		
		String contextRealPath=request.getSession().getServletContext().getRealPath("/");
		
		return contextRealPath;
	}
	
	
	//默认上传地址
	public String getDefUploadPath(){
		
		String defUploadPath=this.getContextRealPath()+"userfiles\\";
		
		return defUploadPath;
	}
	
	//文件上传地址
	public String getfileUploadPath(){
		
		String fileUploadPath=this.getContextRealPath()+"userfiles\\file\\";
		
		return fileUploadPath;
	}
	
	
	//获得图片上传地址
	public String getImgUploadPath(){
		
		String imgUploadPath=this.getContextRealPath()+"userfiles\\image\\";
		
		return imgUploadPath;
		
	}
	
	
	public String getDefWebUrl(String fileName){
		
		String temp=this.getContextVirtualPath()+"/userfiles/"+fileName;
		
		return temp;
	}
	
	
	public String getImgWebUrl(String imgName){
		
		String temp=this.getContextVirtualPath()+"/userfiles/image/"+imgName;
		
		return temp;
	}

}
